package com.xinyin.aiqinhaiback.Utils;

public class ModbusAddressUtil {
	public static int getFunctionCode(int lowAddress) {
		if (lowAddress >= 1 && lowAddress <= 9999) {
			return 1; // 读取线圈状态
		} else if (lowAddress >= 10001 && lowAddress <= 19999) {
			return 2; // 读取离散输入状态
		} else if (lowAddress >= 30001 && lowAddress <= 39999) {
			return 4; // 读取输入寄存器
		} else if (lowAddress >= 40001 && lowAddress <= 49999) {
			return 3; // 读取保持寄存器
		} else {
			throw new IllegalArgumentException("Invalid low address: " + lowAddress);
		}
	}

	public  void main(String[] args){
		System.out.println(getOffset(40047));
	}

	public static int getOffset(int lowAddress) {
		if (lowAddress >= 1 && lowAddress <= 9999) {
			return lowAddress ; // 线圈偏移量
		} else if (lowAddress >= 10001 && lowAddress <= 19999) {
			return lowAddress - 10000; // 离散输入偏移量
		} else if (lowAddress >= 30001 && lowAddress <= 39999) {
			return lowAddress - 30000; // 输入寄存器偏移量
		} else if (lowAddress >= 40001 && lowAddress <= 49999) {
			return lowAddress - 40000; // 保持寄存器偏移量
		} else {
			throw new IllegalArgumentException("Invalid low address: " + lowAddress);
		}
	}



	public static int getOffset2(int highAddress) {
		if (highAddress >= 100000 && highAddress <=109999) {
			return highAddress - 100000; // 线圈偏移量
		} else if (highAddress >= 110000 && highAddress  <= 119999) {
			return highAddress - 110000; // 离散输入偏移量
		} else if (highAddress >=  130000 && highAddress <= 139999) {
			return highAddress - 130000; // 输入寄存器偏移量
		} else if (highAddress >=  140000 && highAddress  <= 149999) {
			return highAddress - 140000; // 保持寄存器偏移量
		} else {
			throw new IllegalArgumentException("Invalid low address: " + highAddress);
		}
	}
}
